//
//  UserAttr_TableVC.m
//  KaChing_App
//
//  Created by Jordan Robert Yust on 2/25/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "UserAttr_TableVC.h"
#import "KaChing_AppAppDelegate.h"
#import "Constants.h"

@implementation UserAttr_TableVC

@synthesize profileName;
@synthesize userID;
@synthesize remoteKey;

- (void)dealloc
{
	[profileName release];
	[userID release];
	[remoteKey release];
    [super dealloc];
}

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad
{
	/* Creates the button for selecting the profile (and specifies the method to be called when the button is clicked) */
	UIBarButtonItem *selectButton = [[UIBarButtonItem alloc] initWithTitle:@"Select" 
																style:UIBarButtonItemStylePlain 
																target:self
																action:@selector(selectButtonClicked)];
	/* Assign the button to the right bar button of the nav controller */
	self.navigationItem.rightBarButtonItem = selectButton;
	[selectButton release];
	
    [super viewDidLoad];
}

/*
 ------------------------------------------------------------------------
 UITableViewDataSource Protocol methods
 ------------------------------------------------------------------------
 */

#pragma mark -
#pragma mark UITableViewDataSource Protocol methods

/*
 Determines how many sections are in the tableView
 NOTE: This method is part of the UITableViewDataSource Protocol.
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

/*
 Determines how many rows are in each section
 NOTE: This method is part of the UITableViewDataSource Protocol.
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

/*
 Is called for each cell in the table.  This method specifies the contents displayed in the cell.
 NOTE: This method is part of the UITableViewDataSource Protocol.
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	/* Obtain the UITableViewCell object at the specified index */
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
	{
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
		
		cell.selectionStyle = UITableViewCellSelectionStyleNone;	// Mark cell as unable to be selected
    }
	
    /* If the cell corresponds to the Profile name */
	if (indexPath.section == profileNameIndex)
	{	
		/* Set the contents of the cell to be the profile name */
		[cell setText:profileName];
	}
	/* If the cell corresponds to the userID */
	else if (indexPath.section == userIDIndex)
	{	
		/* Set the contents of the cell to be the User ID */
		[cell setText:userID];
	}
	/* If the cell corresponds to the remoteKey */
	else if (indexPath.section == remoteKeyIndex)
	{	
		/* Set the contents of the cell to be the Remote Key */
		[cell setText:remoteKey];
	}
	
	/* Return the cell */
    return cell;
}

/*
 Specifies the Header for the appropriate section.
 NOTE: This method is part of the UITableViewDataSource Protocol.
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	if (section == profileNameIndex)
		return @"Profile name";
	else if (section == userIDIndex)
		return @"User ID Number";
	else if (section == remoteKeyIndex)
		return @"User Remote Key";
	else
		return nil;
}


/*
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 }
 if (editingStyle == UITableViewCellEditingStyleInsert) {
 }
 }
 */

/*
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 return YES;
 }
 */

/*
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 return YES;
 }
 */


/*
 ------------------------------------------------------------------------
 UITableViewDelegate Protocol methods
 ------------------------------------------------------------------------
 */

#pragma mark -
#pragma mark UITableViewDelegate Protocol methods

/*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
*/

/*
 ------------------------------------------------------------------------
 UIAlertViewDelegate methods
 ------------------------------------------------------------------------
 */

#pragma mark -
#pragma mark UIAlertViewDelegate methods

/*
 This method is called when one of the buttons in the AlertView is called.
 NOTE: This method is part of the UIAlertViewDelegate Protocol.
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	/* If the button that was pressed was the "Yes" button */
	if ([alertView buttonTitleAtIndex:buttonIndex] == @"Yes")
	{		
		/* Create the DataManager object with the specified userID and remoteKey */
		DataManager *dataManager = [[DataManager alloc] initWithRemoteKey:remoteKey AndUserID:userID];
		
		NSLog(@"\n\nInside User Attr Thing!!\nRemotekey: %@\nUserID: %@", [dataManager remoteKey], [dataManager userID]);
		NSLog(@"UserID as a float: %f", [userID doubleValue]);
		
		/* Passes the DataManager object to the delegate of this class' navigationController (NOTE: The delegate is the Kaching_AppAppDelegate class).
		 This line is crucial to the application.  It allows the DataManager object to be filtered throughout the application so that each class that needs
		 it will be able to access it.  */
		[(KaChing_AppAppDelegate *)[self.navigationController delegate] setDataManager:dataManager];
		
		[dataManager release]; // Since this class no long needs control of it, release the DataManager object 
		
		/* Enter the appDelegates method that enters the user's profile in order to start the main section of the application */
		[(KaChing_AppAppDelegate *)[self.navigationController delegate] enterProfile];
	}
}

/*
 ------------------------------------------------------------------------
Random methods
 ------------------------------------------------------------------------
 */

#pragma mark -
#pragma mark Random methods

/* This method updates the contents of the table view */
- (void) reloadTheData
{
	/* Reloads the table's data */
	[self.tableView reloadData];
}

/*
 This method is called whenever the button to select the profile was selected from the navigation controller
 at the top of the screen. */
- (void) selectButtonClicked
{
	// Create an alertView to confirm selection
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:profileName message:@"Use this profile?"
												   delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];
	[alert show];	
	[alert release];
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
}
*/
/*
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
*/

@end

